package com.simit.web.servlet;

import com.alibaba.fastjson.JSON;
import com.simit.config.SpringConfig;
import com.simit.pojo.YuanShen;
import com.simit.service.YuanShenService;
import com.simit.service.impl.YuanShenServiceImpl1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet("/yuanShen/*")
public class YuanShenServlet extends BaseServlet{
    ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
    YuanShenService yuanShenService = ctx.getBean(YuanShenService.class);

    public void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<YuanShen> yuanShenList = yuanShenService.selectAll();
        String jsonString = JSON.toJSONString(yuanShenList);
        resp.setContentType("text/json;charset=utf-8");
        resp.getWriter().write(jsonString);
    }

    public void selectByName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/json;charset=utf-8");
        String name = req.getParameter("name");

        YuanShen yuanShen = yuanShenService.selectByName(name);
        String jsonString = JSON.toJSONString(yuanShen);
        resp.getWriter().write(jsonString);
    }

    public void selectByConditions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/json;charset=utf-8");

        BufferedReader br = req.getReader();
        String params = br.readLine();
        YuanShen yuanShen = JSON.parseObject(params, YuanShen.class);

        List<YuanShen> yuanShenList = yuanShenService.selectByConditions(yuanShen);
        String jsonString = JSON.toJSONString(yuanShenList);
        resp.getWriter().write(jsonString);
    }

    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/json;charset=utf-8");

        BufferedReader br = req.getReader();
        String params = br.readLine();
        YuanShen yuanShen = JSON.parseObject(params, YuanShen.class);

        if(yuanShenService.selectByName(yuanShen.getName()) == null) {
            yuanShenService.add(yuanShen);
            resp.getWriter().write("添加成功");
        } else {
            resp.getWriter().write("角色已存在");
        }
    }

    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/json;charset=utf-8");

        BufferedReader br = req.getReader();
        String params = br.readLine();
        YuanShen yuanShen = JSON.parseObject(params, YuanShen.class);

        if(yuanShenService.selectByName(yuanShen.getName()) == null) {
            resp.getWriter().write("角色不存在");
        } else {
            yuanShenService.update(yuanShen);
            resp.getWriter().write("修改成功");
        }
    }

    public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/json;charset=utf-8");

        BufferedReader br = req.getReader();
        String params = br.readLine();
        YuanShen yuanShen = JSON.parseObject(params, YuanShen.class);

        if(yuanShenService.selectByName(yuanShen.getName()) == null) {
            resp.getWriter().write("角色不存在");
        } else {
            yuanShenService.delete(yuanShen);
            resp.getWriter().write("删除成功");
        }
    }

    public void deleteByNames(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/json;charset=utf-8");

        BufferedReader br = req.getReader();
        String params = br.readLine();
        String[] names = JSON.parseObject(params, String[].class);

        yuanShenService.deleteByNames(names);
        resp.getWriter().write("删除成功");
    }
}
